package project.vip.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kernel.util.Arith;
import kernel.web.Page;
import kernel.web.PageActionSupport;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import project.order.Order;
import project.order.OrderService;
import project.order.weixi.CommonUtil;
import project.order.weixi.SDKRuntimeException;
import project.order.weixi.WxPayHelper;
import project.order.work.AccessTokenThread;
import project.user.User;
import project.user.UserService;
import project.vip.po.GiftCard;
import project.vip.po.Inv;
import project.vip.po.Vip;
import project.vip.services.AdminGiftCardService;
import project.vip.services.AdminInvService;
import project.vip.services.AdminVipService;
import project.vip.utils.DateUtils;
import project.vip.utils.KBProperties;
import project.vip.utils.SendTplMsgUtils;
import project.vip.utils.StringUtils;
import project.vip.utils.VipWxOpenIdHelper;
import project.vip.utils.VipWxUtils;
import project.vip.utils.bean.WxJsApiBean;
import util.RandomUtils;

public class AdminVipAction extends PageActionSupport {

	private static final Log logger = LogFactory.getLog(AdminVipAction.class);

	/**
	 * 全局urlHttp "http://www.mrshine.cn/sjcf_root"
	 */
	private String gt_url_http = KBProperties.sjcfHttp;

	private AdminVipService adminVipService;

	private OrderService orderService;

	private String id;
	// 用户姓名
	private String name;

	// 手机号码
	private String phone;

	// 收货地址
	private String address;

	private String openId;

	private Date createTime;

	/**
	 * 衬衫券
	 */
	private Integer csTicket = 0;

	/**
	 * 上门券
	 */
	private Integer smTicket = 0;

	/**
	 * 金纽扣
	 */
	private Integer goldBt = 0;

	/**
	 * 邀请函
	 */
	private Integer invNum = 0;
	/**
	 * VIP类别 10:准会员 20:银卡会员 30:金卡会员
	 */
	private String fdType;

	/**
	 * 微信跳转Code
	 */
	private String code;
	/**
	 * 微信跳转state
	 */
	private String state;

	/**
	 * vip卡购买价格
	 */
	private double vipTypePrice;
	/**
	 * 微信支付参数
	 */
	private String bizPackage;
	private Page pageOrder;
	/**
	 * 邀请列表
	 */
	private Page pageInv;
	private AdminGiftCardService adminGiftCardService;
	private Page pageGiftCard;

	private Order order;

	private String orderId;

	private Vip vip = null;

	/**
	 * 发送邀请生成的URL
	 */
	private String invUrl;
	private AdminInvService adminInvService;
	private UserService userService;

	/**
	 * 微信JS－API参数
	 */
	private WxJsApiBean wxJsApiBean;

	/**
	 * 跳转URL
	 */
	private String redirect_url;

	
	public String payOrder() throws SDKRuntimeException {
		String order_body = "";
		HashMap<String, String> tempMap = new HashMap<String, String>();
		if(null != orderId){
			Order order1 = orderService.getOrder(orderId);
			if(null == order1){
				tempMap.put("code", "fail");
				tempMap.put("message", "获取订单失败!");
			}else{

				// 支付参数
				WxPayHelper wxPayHelper = new WxPayHelper();

				/**
				 * 商品描述
				 */
				wxPayHelper.SetParameter("body", "" + order1.getBrand());

				/**
				 * 订单号
				 */
				wxPayHelper.SetParameter("out_trade_no", "sjcfre" + DateUtils.getNowExt1());
				/**
				 * 付款金额
				 */

				if (KBProperties.isTest) {
					wxPayHelper.SetParameter(
							"total_fee",
							String.valueOf(Arith.mul(order1.getPrice(), 100)).substring(
									0,
									String.valueOf(Arith.mul(order1.getPrice(), 100))
											.indexOf(".")));
				} else {
					wxPayHelper.SetParameter("total_fee", String.valueOf(1));
				}

				HttpServletRequest req = (HttpServletRequest) ServletActionContext
						.getRequest();
				String visitorIp = req.getRemoteAddr();
				wxPayHelper.SetParameter("spbill_create_ip", visitorIp);
				wxPayHelper.SetParameter("openid", openId);

				// bizPackage = wxPayHelper.CreateBizPackage();


			

				
				tempMap = wxPayHelper.CreateBizPackageMap();
				// 增加订单号回传
				tempMap.put("orderId", order1.getId().toString());
				if(null != order1.getFd4gold() && order1.getFd4gold().equals("00") && order1.getFd4goldnum() == 0){//这是卡会员购买的订单
					tempMap.put("orderType", "vip");
				}else{
					//商城购买的订单
					if(order1.isSingle()){
						tempMap.put("orderType", "tmall-s");
					}else{
						tempMap.put("orderType", "tmall-ns");
					}
				}
				tempMap.put("code", "success");
			}
		}else{
			tempMap.put("code", "fail");
			tempMap.put("message", "获取订单Order失败!");
		}
		

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(JSONObject.fromObject(tempMap).toString());
			System.out.println("myorder="
					+ JSONObject.fromObject(tempMap).toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	
	/**
	 * 购买礼品卡页面
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String buyGiftCardWx() throws SDKRuntimeException {

		// 通过微信跳转到buyGiftCard
		String wxuri = gt_url_http + "/public/vipAction!buyGiftCard.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl(fdType, wxuri);

		return "buyGiftCardWx";

	}

	public String buyGiftCard() throws SDKRuntimeException {
		HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		fdType = state;
		// if (state.equals("10")) {// 衬衫礼品卡
		//
		// } else if (state.equals("20")) {// 西装礼品卡
		//
		// }

		pageGiftCard = new Page();
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				List<GiftCard> list = adminGiftCardService
						.getGiftCardList(openId);
				pageGiftCard.setElements(list);
				String hideMenuList = "";
				String jsApiList = "'onMenuShareAppMessage','hideMenuItems'";

				hideMenuList = "'menuItem:share:timeline'";
				// 随即码
				String r = RandomUtils.randomNum(6).toString();

				invUrl = "code=" + r + "&openId=" + openId;

				// 获取js－api --分享给朋友的js
				try {
					
					wxJsApiBean = createJsApi(hideMenuList, jsApiList,
							req.getRequestURL() + "?" + req.getQueryString());

				} catch (SDKRuntimeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {

			}
		} else {

		}

		return "buyGiftCard";
	}

	/**
	 * 交易pay成功后执行
	 * 
	 * @return
	 */
	public String updateGiftCard() {
		String message = "交易成功!";
		GiftCard giftCard = adminGiftCardService.getById(orderId);
		if (null != giftCard) {
			giftCard.setFdUuid("20");// 付款
			giftCard.setModifyTime(new Date());
		} else {
			message = "礼品卡购买失败!";
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(message);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String payGiftCardWx() throws SDKRuntimeException {

		// 通过微信跳转到buyGiftCard
		String wxuri = gt_url_http + "/public/vipAction!payGiftCard.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl(orderId, wxuri);

		return "payGiftCardWx";

	}

	/**
	 * 支付页面
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String payGiftCard() throws SDKRuntimeException {
		HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		if(null == openId){
			Object o = req.getSession().getAttribute("curOpenId");//获取session中的
			if(null != o){
				openId = o.toString();
			}
		}
		req.getSession().setAttribute("curOpenId", openId);//设置到session中
		// 增加user
		User user = userService.find(openId);
		if (user != null) {
			user.setName(name);
			user.setPhone(phone);
			userService.update(user);
		} else {
			user = new User();
			user.setName(name);
			user.setPhone(phone);
			user.setOpenId(openId);
			userService.save(user);
		}

		String order_body = "";
		// 判断
		String message = "";
		String code = "success";

		GiftCard giftCard = adminGiftCardService.getById(orderId);
		if (null != giftCard) {
			order_body = "您的礼品卡赠送给[" + giftCard.getToName() + "]";
			if (giftCard.getToOpenId().equals("10")) {
				vipTypePrice = new Double(KBProperties.sjcfGiftCardTpe10);
			} else if (giftCard.getToOpenId().equals("10")) {
				vipTypePrice = new Double(KBProperties.sjcfGiftCardTpe20);
			} else {
				vipTypePrice = 0.01;
			}

			// 支付参数
			WxPayHelper wxPayHelper = new WxPayHelper();

			/**
			 * 商品描述
			 */
			wxPayHelper.SetParameter("body", "" + order_body);

			/**
			 * 订单号
			 */
			wxPayHelper.SetParameter("out_trade_no",
					"sjcfGiftCard" + DateUtils.getNowExt1());
			/**
			 * 付款金额
			 */
			wxPayHelper.SetParameter(
					"total_fee",
					String.valueOf(Arith.mul(vipTypePrice, 100)).substring(
							0,
							String.valueOf(Arith.mul(vipTypePrice, 100))
									.indexOf(".")));

			
			String visitorIp = req.getRemoteAddr();
			wxPayHelper.SetParameter("spbill_create_ip", visitorIp);
			wxPayHelper.SetParameter("openid", openId);

			HashMap<String, String> tempMap = new HashMap<String, String>();
			tempMap = wxPayHelper.CreateBizPackageMap();
			// 增加订单号回传
			tempMap.put("orderId", orderId);
			tempMap.put("code", code);
			tempMap.put("message", message);

			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out;
			try {
				out = response.getWriter();
				out.println(JSONObject.fromObject(tempMap).toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		} else {
			HashMap<String, String> tempMap = new HashMap<String, String>();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out;
			try {
				tempMap.put("code", "fail");
				tempMap.put("message", "找不到礼品单[" + orderId + "]");
				out = response.getWriter();
				out.println(JSONObject.fromObject(tempMap).toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

	}

	/**
	 * 交易pay成功后执行
	 * 
	 * @return
	 */
	public String updateVip() {
		String message = "交易成功!";
		Vip vip = adminVipService.find(openId);
//		System.out.println("openId="+openId);
		if (vip != null) {
			if (fdType.equals("20")) {
				vip.setInvNum(vip.getInvNum() + 5);
				vip.setGoldBt(vip.getGoldBt() + 5);
				// vip.setCsTicket(vip.getCsTicket()+0);
				vip.setSmTicket(vip.getSmTicket() + 1);
			} else if (fdType.equals("30")) {
				vip.setInvNum(vip.getInvNum() + 5);
				vip.setGoldBt(vip.getGoldBt() + 20);
				vip.setCsTicket(vip.getCsTicket() + 2);
				vip.setSmTicket(vip.getSmTicket() + 5);
			} else if (fdType.equals("50")) {// 闪耀会员

				vip.setInvNum(vip.getInvNum() + KBProperties.vip50invNum);
				vip.setGoldBt(vip.getGoldBt() + KBProperties.vip50goldBt);
				vip.setCsTicket(vip.getCsTicket() + KBProperties.vip50csTicket);
				vip.setSmTicket(vip.getSmTicket() + KBProperties.vip50SmTicket);
			}
			vip.setFdType(fdType);
			adminVipService.update(vip);
			// 更新orderId
			Order order = orderService.getOrder(orderId);
			if (order != null) {
				order.setPayment(1);
				order.setFirmOrder("1");
				orderService.update(order);
			}
//			System.out.println("fdType="+fdType);
			// 根据toPhone查找邀请表，如果存在未支付状态的给 发送邀请方 fromOpenId 增加福利：
			// 4、发出邀请后 朋友接受邀请，并充值成为银卡。邀请人获得 一张衬衫券 一枚金纽扣 一张邀请函。
			// 5、发出邀请后 朋友接受邀请，并充值成为金卡。邀请人获得两张衬衫券 一枚金纽扣 两张邀请函 一张上门券。
			List<Inv> list = adminInvService.getInvList(vip.getPhone(),false);
			for (int i = 0; i < list.size(); i++) {
				Inv inv = list.get(0);
//				System.out.println("inv="+inv.getToPhone());
				//增加邀请人的福利
				Vip cVip = adminVipService.find(inv.getFromOpenId());
				if (cVip != null) {
					if (fdType.equals("20")) {
						cVip.setInvNum(cVip.getInvNum() + 1);
						cVip.setGoldBt(cVip.getGoldBt() + 1);
						cVip.setCsTicket(cVip.getCsTicket() + 1);
					} else if (fdType.equals("30")) {
						cVip.setInvNum(cVip.getInvNum() + 2);
						cVip.setGoldBt(cVip.getGoldBt() + 1);
						cVip.setCsTicket(cVip.getCsTicket() + 2);
						cVip.setSmTicket(cVip.getSmTicket() + 1);
					} else if (fdType.equals("50")) {
						cVip.setInvNum(cVip.getInvNum() + 2);
						cVip.setGoldBt(cVip.getGoldBt() + 5);
						cVip.setCsTicket(cVip.getCsTicket() + 1);
					}
					adminVipService.update(cVip);
				}
				//增加被邀请人福利 金纽扣＋1
				Vip bVip = adminVipService.find(openId);
				bVip.setGoldBt(bVip.getGoldBt() + 1);
				adminVipService.update(bVip);
				
				//更新邀请列表状态
				inv.setIsDeposit(true);
				inv.setToOpenId(openId);
				adminInvService.update(inv);
			}
		} else {
			message = "找不到该vip!";
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(message);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 支付页面
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String pay() throws SDKRuntimeException {
		String order_body = "";
		if (fdType.equals("20")) {// 银卡
			invNum = 5;
			goldBt = 5;
			csTicket = 0;
			smTicket = 1;
			vipTypePrice = KBProperties.vip20Price;
			order_body = "私家裁缝-银卡";
		} else if (fdType.equals("30")) {// 金卡
			invNum = 5;
			goldBt = 20;
			csTicket = 2;
			smTicket = 5;
			vipTypePrice = KBProperties.vip30Price;
			order_body = "私家裁缝-金卡";
		} else if (fdType.equals("50")) {// 闪耀会员
			invNum = KBProperties.vip50invNum;
			goldBt = KBProperties.vip50goldBt;
			csTicket = KBProperties.vip50csTicket;
			smTicket = KBProperties.vip50SmTicket;
			vipTypePrice = KBProperties.vip50price;
			order_body = "私家裁缝-闪耀会员卡";
		}
		// 增加user
		User user = userService.find(openId);
		if (user != null) {
			user.setName(name);
			user.setPhone(phone);
			userService.update(user);
		} else {
			user = new User();
			user.setName(name);
			user.setPhone(phone);
			user.setOpenId(openId);
			userService.save(user);
		}
		// 增加vip
		Vip vip = adminVipService.find(openId);
		if (vip != null) {
			vip.setName(name);
			vip.setPhone(phone);
			if (StringUtils.isStringEmpty(vip.getFdType())) {
				vip.setFdType("10");// 准会员
			}
			adminVipService.update(vip);
		} else {
			vip = new Vip();
			vip.setName(name);
			vip.setPhone(phone);
			vip.setOpenId(openId);
			if (StringUtils.isStringEmpty(vip.getFdType())) {
				vip.setFdType("00");// 非会员
			}
			adminVipService.save(vip);
		}
		// 支付参数
		WxPayHelper wxPayHelper = new WxPayHelper();

		/**
		 * 商品描述
		 */
		wxPayHelper.SetParameter("body", "" + order_body);

		/**
		 * 订单号
		 */
		wxPayHelper.SetParameter("out_trade_no",
				"sjcfvip" + DateUtils.getNowExt1());
		/**
		 * 付款金额
		 */

		if (KBProperties.isTest) {
			wxPayHelper.SetParameter(
					"total_fee",
					String.valueOf(Arith.mul(vipTypePrice, 100)).substring(
							0,
							String.valueOf(Arith.mul(vipTypePrice, 100))
									.indexOf(".")));
		} else {
			wxPayHelper.SetParameter("total_fee", String.valueOf(1));
		}

		HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		String visitorIp = req.getRemoteAddr();
		wxPayHelper.SetParameter("spbill_create_ip", visitorIp);
		wxPayHelper.SetParameter("openid", openId);

		// bizPackage = wxPayHelper.CreateBizPackage();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;

		// 创建订单Order order = new Order();
		Order order = new Order();
		order.setText(wxPayHelper.GetParameter("out_trade_no"));// 微信订单
		order.setName(name);
		order.setAddress(address);
		order.setPhone(phone);

		order.setBrand(wxPayHelper.GetParameter("body"));
		// order.setBrand_img();
		order.setPayment(0);
		order.setOpenId(openId);
		order.setFirmOrder("0");
		order.setPrice(vipTypePrice);
		order.setFd4gold("00");//设置金纽扣不使用
		order.setFd4goldnum(0);//设置金纽扣数量0 为了区别是购买的卡会员而不是购买商城的商品
		order.setSingle(true);
		orderService.save(order);

		HashMap<String, String> tempMap = new HashMap<String, String>();
		tempMap = wxPayHelper.CreateBizPackageMap();
		// 增加订单号回传
		tempMap.put("orderId", order.getId().toString());

		try {
			out = response.getWriter();
			out.println(JSONObject.fromObject(tempMap).toString());
			System.out.println("vip_order="
					+ JSONObject.fromObject(tempMap).toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}



	public String sendGiftCardWx() throws SDKRuntimeException {

		// 通过微信跳转到sendInv
		String wxuri = gt_url_http + "/public/vipAction!sendGiftCard.action?fromOpenId="+openId+"&fromCode="+code;
		redirect_url = VipWxOpenIdHelper.getCodeUrl("sendGiftCard", wxuri);

		return "sendGiftCardWx";

	}
	
	/**
	 * 发送礼品
	 * 
	 * @return
	 */
	public String sendGiftCard() {
		HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		System.out.println("openId---"+openId);
		// 通过code + openId 查询是否已经接受邀请

		String fromOpenId = req.getParameter("fromOpenId");
		String fromCode = req.getParameter("fromCode");
		
		List<GiftCard> list = adminGiftCardService
				.getGiftCardList(fromOpenId, fromCode);

		// 获取openId对应的 姓名 手机 地址
		User userInfo = userService.find(openId);
		if (userInfo != null) {
			name = userInfo.getName();
			phone = userInfo.getPhone();
			address = userInfo.getAddress();
		}
		if (list.size() == 1) {
			
			req.setAttribute("fromName", list.get(0).getFromName());
			fdType = list.get(0).getToOpenId();
			if (DateUtils.getDayDiffer(DateUtils.convertDateToString(list
					.get(0).getCreateTime(), DateUtils.PATTERN_DATETIME),
					DateUtils.getNow()) <= KBProperties.sjcfGiftCardTimeout) {
				if (list.get(0).getFdUuid().equals("00")) {
					// 未接受邀请
					req.setAttribute("subMode", "00");

					req.setAttribute("fromCode", fromCode);
					req.setAttribute("fromOpenId", fromOpenId);
				} else {
					// 已经接受邀请
					req.setAttribute("subMode", "10");
				}
			} else {
				// 已经失效
				req.setAttribute("subMode", "10");
			}

		} else {
			// 邀请错误
		}
		return "sendGiftCard";
	}

	public String sendInvWx() throws SDKRuntimeException {

		// 通过微信跳转到sendInv
		String wxuri = gt_url_http + "/public/vipAction!sendInv.action?fromOpenId="+openId+"&fromCode="+code;
		redirect_url = VipWxOpenIdHelper.getCodeUrl("sendInv", wxuri);

		return "sendInvWx";

	}
	/**
	 * 发送邀请
	 * 
	 * @return
	 */
	public String sendInv() {
		HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		
		// 通过code + fromOpenId 查询是否已经接受邀请
		String fromOpenId = req.getParameter("fromOpenId");
		String fromCode = req.getParameter("fromCode");

//		System.out.println("fromCode="+fromCode+"-fromOpenId"+fromOpenId);
		if(null == fromOpenId || fromOpenId.equals("") || null == fromCode || fromCode.equals("")){
			req.setAttribute("subMode", "10");//理论上不会到这里
		}else{
			req.setAttribute("fromCode", fromCode);
			req.setAttribute("fromOpenId", fromOpenId);
			List<Inv> listed = adminInvService.getInvList4openId(openId);
//			System.out.println("openId"+openId);
			// 获取openId 上次填的邀请 对应的 姓名 手机 地址
			if (listed.size() > 0) {
				name = listed.get(0).getToName();
				phone = listed.get(0).getToPhone();
//				System.out.println("name"+name);
			}
		}

		List<Inv> list = adminInvService.getInvList(fromOpenId,fromCode);
		
		if (list.size() == 1) {
			if (DateUtils.getDayDiffer(DateUtils.convertDateToString(list
					.get(0).getCreateTime(), DateUtils.PATTERN_DATETIME),
					DateUtils.getNow()) <= KBProperties.sjcfGiftCardTimeout) {
				if (!list.get(0).getIsInv()) {
					// 未接受邀请
					req.setAttribute("subMode", "00");
				} else {
					// 已经接受邀请
					req.setAttribute("subMode", "10");
				}
			} else {
				// 已经失效
				req.setAttribute("subMode", "10");
			}
			
			

		} else {
			// 邀请错误
		}
		return "sendInv";
	}
	/**
	 * 发送邀请
	 * 
	 * @return
	 */
//	public String sendInv() {
//		
//
//		List<Inv> list = adminInvService.getInvList(vip.getPhone());
//		
//		if (list.size() == 1) {
//			HttpServletRequest req = (HttpServletRequest) ServletActionContext
//					.getRequest();
//			if (DateUtils.getDayDiffer(DateUtils.convertDateToString(list
//					.get(0).getCreateTime(), DateUtils.PATTERN_DATETIME),
//					DateUtils.getNow()) <= KBProperties.sjcfGiftCardTimeout) {
//				if (!list.get(0).getIsInv()) {
//					// 未接受邀请
//					req.setAttribute("subMode", "00");
//				} else {
//					// 已经接受邀请
//					req.setAttribute("subMode", "10");
//				}
//			} else {
//				// 已经失效
//				req.setAttribute("subMode", "10");
//			}
//			
//			
//
//		} else {
//			// 邀请错误
//		}
//		return "sendInv";
//	}
	/**
	 * 扣除邀请数
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String moveInv() throws UnsupportedEncodingException {
		HashMap<String, String> nativeObj = new HashMap<String, String>();
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {

				// 更新vip的邀请数
				//vip = adminVipService.find(openId);
				if (vip != null) {
					if (vip.getInvNum() - 1 < 0) {
						nativeObj.put("code", "fail");
						nativeObj.put("message", "扣除邀请1失败-剩余邀请数不足");
					} else {
						vip.setInvNum(vip.getInvNum() - 1);
						adminVipService.update(vip);
						nativeObj.put("code", "success");
						nativeObj.put("message", "扣除邀请1");

						// 增加邀请纪录
						Inv item = new Inv();
						item.setFromOpenId(openId);
						item.setFromPhone(code);
						item.setFromName(vip.getName());
						adminInvService.save(item);
					}
				} else {
					nativeObj.put("code", "fail");
					nativeObj.put("message", "vip找不到");
				}

			} else {
				// 找不到vip
			}
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(JSONObject.fromObject(nativeObj).toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String moveGiftCard() throws UnsupportedEncodingException {
		HashMap<String, String> nativeObj = new HashMap<String, String>();
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				
				//减少礼品卡数量
				if (vip.getCsTicket() - 1 < 0) {
					nativeObj.put("code", "fail");
					nativeObj.put("message", "扣除礼品卡1失败-剩余礼品卡数不足");
				} else {
					vip.setCsTicket(vip.getCsTicket() - 1);
					adminVipService.update(vip);
					nativeObj.put("code", "success");
					nativeObj.put("message", "扣除礼品卡1");
					// 增加礼品卡纪录
					GiftCard item = new GiftCard();
					item.setFromName(vip.getName());
					item.setFromOpenId(openId);
					item.setFromPhone(code);
					item.setToOpenId(fdType);// 设置礼品的类别 10 OR 20
					item.setFdUuid("00");// 未接受
					adminGiftCardService.save(item);
				}
				
			} else {
				// 找不到openid对应的人员
			}
		}


		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(JSONObject.fromObject(nativeObj).toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 不用
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String revInvWx() throws SDKRuntimeException {

		// 通过微信跳转到buyVip
		String wxuri = gt_url_http + "/public/vipAction!revInv.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl("revInv", wxuri);

		return "revInvWx";

	}

	/**
	 * 接收邀请
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String revInv() throws UnsupportedEncodingException {

		
		
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				List<Inv> list = adminInvService.getInvList(openId, code);
				if (list.size() == 1) {
					Inv inv = list.get(0);
					if (inv.getIsInv()) {
						// 已经接受邀请
					} else {
						// 未接受邀请
						// 更新邀请标志
						inv.setIsInv(true);
						inv.setModifyTime(new Date());
						inv.setToName(java.net.URLDecoder.decode(name, "UTF-8"));
						inv.setToPhone(phone);
//						inv.setToOpenId(code);
						adminInvService.update(inv);
						// 更新vip的金纽扣数
//						vip = adminVipService.find(openId);
//						if (vip != null) {
//							vip.setGoldBt(vip.getGoldBt() + 1);
//							if (StringUtils.isStringEmpty(vip.getFdType())) {
//								vip.setFdType("10");// 准会员
//							}
//							adminVipService.update(vip);
//						} else {
//							vip = new Vip();
//							vip.setName(name);
//							vip.setPhone(phone);
//							vip.setGoldBt(1);
//							vip.setOpenId(openId);
//							if (StringUtils.isStringEmpty(vip.getFdType())) {
//								vip.setFdType("10");// 准会员
//							}
//							adminVipService.save(vip);
//						}

					}

				} else {
					// 邀请错误
				}
			} else {
				// 找不到发送方
			}
		}

		return "focusSjcf";
	}

	/**
	 * 
	 * 不使用，因为礼品卡用完找客服
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String revGiftCardWx() throws SDKRuntimeException {

		// 通过微信跳转到revGiftCard
		String wxuri = gt_url_http + "/public/vipAction!revGiftCard.action?fromOpenId="+openId+"&fromCode="+code;
		redirect_url = VipWxOpenIdHelper.getCodeUrl("revGiftCard", wxuri);

		return "revGiftCardWx";

	}

	/**
	 * 接收礼品卡
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String revGiftCard() throws UnsupportedEncodingException {

		
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			System.out.println("-22-"+openId);
			if (null != vip) {
				List<GiftCard> list = adminGiftCardService.getGiftCardList(
						openId, code);
				if (list.size() == 1) {
					GiftCard gift = list.get(0);
					if (gift.getFdUuid().equals("00")) {
						// 未接受邀请
						// 更新邀请标志
						gift.setModifyTime(new Date());
						gift.setToName(java.net.URLDecoder
								.decode(name, "UTF-8"));
						gift.setToPhone(phone);
//						gift.setToOpenId(openId);
						gift.setFdUuid("10");
						adminGiftCardService.update(gift); 
						
						//增加接受方的衬衫券个数
//						vip.setCsTicket(vip.getCsTicket() + 1);
//						if (StringUtils.isStringEmpty(vip.getFdType())) {
//							vip.setFdType("10");// 准会员
//						}
//						adminVipService.update(vip);

						//发送消息模版
//						LinkedHashMap<String, String> dataMap = new LinkedHashMap<String, String>();
//						dataMap.put("first", "您有一个礼品被["+name+"-手机号码:"+phone+"]接受了.");
//						dataMap.put("keyword1", "2");
//						dataMap.put("keyword2", "1");
//						dataMap.put("keyword3", "3");
//						dataMap.put("keyword4", DateUtils.getNow());
//						dataMap.put("remark", "备注：2周内未被领取的礼品将退回!");
//						
//						SendTplMsgUtils.sendCommonTpl(gift.getFromOpenId(), "8lY2VO7JkvEf2bS7eSJ4nomIj8Ce1pL55LhHI0-mpDo", dataMap);
					} else {
						// 已经接受邀请

					}

				} else {
					// 邀请错误
				}
			} else {
				// 找不到发送方
			}
		}

		return "focusSjcfGift";
	}

	/**
	 * 我的订单详情
	 * 
	 * @return
	 */
	public String myOrderDetail() {
		order = adminVipService.findOrder(orderId);
		return "myOrderDetail";

	}

	/**
	 * 发送邀请列表wx url
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String sendInvListWx() throws SDKRuntimeException {

		// 通过微信跳转到goVipCenter
		String wxuri = gt_url_http + "/public/vipAction!sendInvList.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl("sendInvList", wxuri);

		return "sendInvListWx";

	}

	/**
	 * 发送邀请列表
	 * 
	 * @return
	 */
	public String sendInvList() {
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);

			HttpServletRequest req = (HttpServletRequest) ServletActionContext
					.getRequest();
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		pageInv = new Page();
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				pageInv = adminVipService.getSendInvList(pageNo, pageSize,
						name, phone, null, null, null, openId);
				String hideMenuList = "";
				String jsApiList = "'onMenuShareAppMessage','hideMenuItems'";
				if (vip.getInvNum() > 0) {
					hideMenuList = "'menuItem:share:timeline'";
					// 随即码
					String r = RandomUtils.randomNum(6).toString();

					invUrl = "code=" + r + "&openId=" + openId;
				} else {
					hideMenuList = "'menuItem:share:timeline','menuItem:share:appMessage'";
				}
				// 获取js－api --分享给朋友的js
				try {
					HttpServletRequest req = (HttpServletRequest) ServletActionContext
							.getRequest();
					wxJsApiBean = createJsApi(hideMenuList, jsApiList,
							req.getRequestURL() + "?" + req.getQueryString());

				} catch (SDKRuntimeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return "sendInvList";
			} else {
				return "sendInvListNull";
			}
		} else {
			return "sendInvListNull";
		}
	}

	public String myOrderWx() throws SDKRuntimeException {

		// 通过微信跳转到goVipCenter
		String wxuri = gt_url_http + "/public/vipAction!myOrder.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl("myOrder", wxuri);

		return "myOrderWx";

	}

	/**
	 * 我的订单
	 * 
	 * @return
	 */
	public String myOrder() {
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);

			HttpServletRequest req = (HttpServletRequest) ServletActionContext
					.getRequest();
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		pageOrder = new Page();
		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				pageOrder = adminVipService.myOrder(pageNo, pageSize, name,
						phone, null, null, openId);
				return "myOrder";
			} else {
				return "myOrderNull";
			}
		} else {
			return "myOrderNull";
		}
	}

	/**
	 * 购买会员页面
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String buyVipWx() throws SDKRuntimeException {

		// 通过微信跳转到buyVip
		String wxuri = gt_url_http + "/public/vipAction!buyVip.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl(fdType, wxuri);

		return "next";

	}

	public String buyVip() throws SDKRuntimeException {
		fdType = state;
		if (state.equals("20")) {// 银卡
			invNum = 5;
			goldBt = 5;
			csTicket = 0;
			smTicket = 1;
			vipTypePrice = KBProperties.vip20Price;

		} else if (state.equals("30")) {// 金卡
			invNum = 5;
			goldBt = 20;
			csTicket = 2;
			smTicket = 5;
			vipTypePrice = KBProperties.vip30Price;
		} else if (state.equals("50")) {// 闪耀会员
			invNum = KBProperties.vip50invNum;
			goldBt = KBProperties.vip50goldBt;
			csTicket = KBProperties.vip50csTicket;
			smTicket = KBProperties.vip50SmTicket;
			vipTypePrice = KBProperties.vip50price;
		}
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);

			HttpServletRequest req = (HttpServletRequest) ServletActionContext
					.getRequest();
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		// 获取openId对应的 姓名 手机 地址
		User userInfo = userService.find(openId);
		if (userInfo != null) {
			name = userInfo.getName();
			phone = userInfo.getPhone();
			address = userInfo.getAddress();
		}
		return "buyVip";
	}

	/**
	 * 跳转goVipCenterWxBuy
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String goVipCenterWxBuy() throws SDKRuntimeException {

		// 通过微信跳转到goVipCenter
		String wxuri = gt_url_http + "/public/vipAction!goVipCenterBuy.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl("goVipCenterBuy", wxuri);

		return "goVipCenterWxBuy";

	}

	/**
	 * 跳转到VIP中心 
	 * 
	 * @return
	 */
	public String goVipCenterBuy() {
		
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);
			HttpServletRequest req = (HttpServletRequest) ServletActionContext
					.getRequest();
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
			
			JSONObject jsonOb = VipWxOpenIdHelper.getJsonObGet(openId);
			if (null != jsonOb.get("headimgurl")) {
				req.setAttribute("headimgurl",
						(String) jsonOb.get("headimgurl"));
			}
			if (null != jsonOb.get("nickname")) {
				req.setAttribute("nickname", (String) jsonOb.get("nickname"));
			}

		}

		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				name = vip.getName();
				address = vip.getAddress();
				createTime = vip.getCreateTime();
				csTicket = vip.getCsTicket();
				fdType = vip.getFdType();
				goldBt = vip.getGoldBt();
				invNum = vip.getInvNum();
				openId = vip.getOpenId();
				phone = vip.getPhone();
				smTicket = vip.getSmTicket();

				return "view00";
			} else {
				// 非会员
				return "view00";
			}
		} else {
			// 非会员
			return "view00";
		}

	}

	/**
	 * 跳转goVipCenter
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String goVipCenterWx() throws SDKRuntimeException {

		// 通过微信跳转到goVipCenter
		String wxuri = gt_url_http + "/public/vipAction!goVipCenter.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl("goVipCenter", wxuri);

		return "goVipCenterWx";

	}

	/**
	 * 跳转到VIP中心
	 * 
	 * @return
	 */
	public String goVipCenter() {
		if (KBProperties.isTest) {

			HttpServletRequest req = (HttpServletRequest) ServletActionContext
					.getRequest();
			openId = VipWxOpenIdHelper.getOpenId(code);
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
			JSONObject jsonOb = VipWxOpenIdHelper.getJsonObGet(openId);
			if (null != jsonOb.get("headimgurl")) {
				req.setAttribute("headimgurl",
						(String) jsonOb.get("headimgurl"));
			}
			if (null != jsonOb.get("nickname")) {
				req.setAttribute("nickname", (String) jsonOb.get("nickname"));
			}

		}

		if (!StringUtils.isStringEmpty(openId)) {
			vip = adminVipService.find(openId);
			if (null != vip) {
				name = vip.getName();
				address = vip.getAddress();
				createTime = vip.getCreateTime();
				csTicket = vip.getCsTicket();
				fdType = vip.getFdType();
				goldBt = vip.getGoldBt();
				invNum = vip.getInvNum();
				openId = vip.getOpenId();
				phone = vip.getPhone();
				smTicket = vip.getSmTicket();

				if (vip.getFdType().equals("00")) {// 有关注，但不是会员需要成为会员

					return "view00";
				} else if (vip.getFdType().equals("10")) {// 准会员

					return "view10";
				} else if (vip.getFdType().equals("20")) {// 银卡会员 取消

					return "view10";
				} else if (vip.getFdType().equals("30")) {// 金卡会员 取消

					return "view10";
				} else if (vip.getFdType().equals("40")) {// VIP会员

					return "view40";
				} else if (vip.getFdType().equals("50")) {// 闪耀会员

					return "view50";
				}
			} else {
				// 非会员
				return "view00";
			}
		} else {
			// 非会员
			return "view00";
		}

		return "view";

	}

	public String view() {
		this.pageSize = 20;
		page = adminVipService.pagedQuery(pageNo, pageSize, phone);
		super.page = page;
		this.tabs = this.bulidTabs();
		return "view";

	}

	public String toAdd() {
		return "add";
	}

	public void setAdminVipService(AdminVipService adminVipService) {
		this.adminVipService = adminVipService;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the openId
	 */
	public String getOpenId() {
		return openId;
	}

	/**
	 * @param openId
	 *            the openId to set
	 */
	public void setOpenId(String openId) {
		this.openId = openId;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime
	 *            the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @return the csTicket
	 */
	public Integer getCsTicket() {
		return csTicket;
	}

	/**
	 * @param csTicket
	 *            the csTicket to set
	 */
	public void setCsTicket(Integer csTicket) {
		this.csTicket = csTicket;
	}

	/**
	 * @return the smTicket
	 */
	public Integer getSmTicket() {
		return smTicket;
	}

	/**
	 * @param smTicket
	 *            the smTicket to set
	 */
	public void setSmTicket(Integer smTicket) {
		this.smTicket = smTicket;
	}

	/**
	 * @return the goldBt
	 */
	public Integer getGoldBt() {
		return goldBt;
	}

	/**
	 * @param goldBt
	 *            the goldBt to set
	 */
	public void setGoldBt(Integer goldBt) {
		this.goldBt = goldBt;
	}

	/**
	 * @return the invNum
	 */
	public Integer getInvNum() {
		return invNum;
	}

	/**
	 * @param invNum
	 *            the invNum to set
	 */
	public void setInvNum(Integer invNum) {
		this.invNum = invNum;
	}

	/**
	 * @return the fdType
	 */
	public String getFdType() {
		return fdType;
	}

	/**
	 * @param fdType
	 *            the fdType to set
	 */
	public void setFdType(String fdType) {
		this.fdType = fdType;
	}

	/**
	 * @return the adminVipService
	 */
	public AdminVipService getAdminVipService() {
		return adminVipService;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the vipTypePrice
	 */
	public double getVipTypePrice() {
		return vipTypePrice;
	}

	/**
	 * @param vipTypePrice
	 *            the vipTypePrice to set
	 */
	public void setVipTypePrice(double vipTypePrice) {
		this.vipTypePrice = vipTypePrice;
	}

	/**
	 * @return the pageOrder
	 */
	public Page getPageOrder() {
		return pageOrder;
	}

	/**
	 * @param pageOrder
	 *            the pageOrder to set
	 */
	public void setPageOrder(Page pageOrder) {
		this.pageOrder = pageOrder;
	}

	/**
	 * @return the order
	 */
	public Order getOrder() {
		return order;
	}

	/**
	 * @param order
	 *            the order to set
	 */
	public void setOrder(Order order) {
		this.order = order;
	}

	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId
	 *            the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the pageInv
	 */
	public Page getPageInv() {
		return pageInv;
	}

	/**
	 * @param pageInv
	 *            the pageInv to set
	 */
	public void setPageInv(Page pageInv) {
		this.pageInv = pageInv;
	}

	/**
	 * @return the vip
	 */
	public Vip getVip() {
		return vip;
	}

	/**
	 * @param vip
	 *            the vip to set
	 */
	public void setVip(Vip vip) {
		this.vip = vip;
	}

	/**
	 * @return the invUrl
	 */
	public String getInvUrl() {
		return invUrl;
	}

	/**
	 * @param invUrl
	 *            the invUrl to set
	 */
	public void setInvUrl(String invUrl) {
		this.invUrl = invUrl;
	}

	/**
	 * @return the adminInvService
	 */
	public AdminInvService getAdminInvService() {
		return adminInvService;
	}

	/**
	 * @param adminInvService
	 *            the adminInvService to set
	 */
	public void setAdminInvService(AdminInvService adminInvService) {
		this.adminInvService = adminInvService;
	}

	/**
	 * @return the wxJsApiBean
	 */
	public WxJsApiBean getWxJsApiBean() {
		return wxJsApiBean;
	}

	/**
	 * @param wxJsApiBean
	 *            the wxJsApiBean to set
	 */
	public void setWxJsApiBean(WxJsApiBean wxJsApiBean) {
		this.wxJsApiBean = wxJsApiBean;
	}

	/**
	 * 创建JS－API
	 * 
	 * @return
	 * @throws SDKRuntimeException
	 */
	private WxJsApiBean createJsApi(String hideMenuList, String jsApiList,
			String url) throws SDKRuntimeException {
		WxJsApiBean jsBean = new WxJsApiBean();
		jsBean.setAppId(AccessTokenThread.appid);
		jsBean.setJsApiList(jsApiList);
		jsBean.setHideMenuList(hideMenuList);
		jsBean.setNonceStr(CommonUtil.CreateNoncestr());
		jsBean.setTimestamp(Long.toString(new Date().getTime() / 1000));
		// --组装成加密信息:签名生成规则如下：参与签名的字段包括noncestr（随机字符串）, 有效的jsapi_ticket,
		// timestamp（时间戳）,
		// --url（当前网页的URL，不包含#及其后面部分） 。对所有待签名参数按照字段名的ASCII
		// 码从小到大排序（字典序）后，使用URL键值对的格式（即key1=value1&key2=value2…）
		// --拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密，字段名和字段值都采用原始值，不进行URL
		// 转义。
		HashMap<String, String> nativeObj = new HashMap<String, String>();
		nativeObj.put("noncestr", jsBean.getNonceStr());
		nativeObj.put("jsapi_ticket", AccessTokenThread.jsapi_ticket);
		nativeObj.put("timestamp", jsBean.getTimestamp());
		nativeObj.put("url", url);
		// --进行签名
		jsBean.setSignature(GetJsAPISign(nativeObj));

		return jsBean;
	}

	private String GetJsAPISign(HashMap<String, String> bizObj)
			throws SDKRuntimeException {
		HashMap<String, String> bizParameters = new HashMap<String, String>();

		List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(
				bizObj.entrySet());

		Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
			public int compare(Map.Entry<String, String> o1,
					Map.Entry<String, String> o2) {
				return (o1.getKey()).toString().compareTo(o2.getKey());
			}
		});

		for (int i = 0; i < infoIds.size(); i++) {
			Map.Entry<String, String> item = infoIds.get(i);
			if (item.getKey() != "") {
				// bizParameters.put(item.getKey().toLowerCase(),
				// item.getValue());
				bizParameters.put(item.getKey(), item.getValue());
			}
		}

		String bizString = CommonUtil.FormatBizQueryParaMap(bizParameters,
				false);

		// return SHA1Util.Sha1(bizString);
		return VipWxUtils.Sha1(bizString);

	}

	/**
	 * @return the bizPackage
	 */
	public String getBizPackage() {
		return bizPackage;
	}

	/**
	 * @param bizPackage
	 *            the bizPackage to set
	 */
	public void setBizPackage(String bizPackage) {
		this.bizPackage = bizPackage;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the redirect_url
	 */
	public String getRedirect_url() {
		return redirect_url;
	}

	/**
	 * @param redirect_url
	 *            the redirect_url to set
	 */
	public void setRedirect_url(String redirect_url) {
		this.redirect_url = redirect_url;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @param state
	 *            the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @return the adminGiftCardService
	 */
	public AdminGiftCardService getAdminGiftCardService() {
		return adminGiftCardService;
	}

	/**
	 * @param adminGiftCardService
	 *            the adminGiftCardService to set
	 */
	public void setAdminGiftCardService(
			AdminGiftCardService adminGiftCardService) {
		this.adminGiftCardService = adminGiftCardService;
	}

	/**
	 * @return the pageGiftCard
	 */
	public Page getPageGiftCard() {
		return pageGiftCard;
	}

	/**
	 * @param pageGiftCard
	 *            the pageGiftCard to set
	 */
	public void setPageGiftCard(Page pageGiftCard) {
		this.pageGiftCard = pageGiftCard;
	}

	/**
	 * @return the orderService
	 */
	public OrderService getOrderService() {
		return orderService;
	}

	/**
	 * @param orderService
	 *            the orderService to set
	 */
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

}
